import Region from '@/utils/region.json';

export default {
	_SetStorage: function(name, key) {
		uni.setStorageSync(name, key)
	},
	_GetStorage: function(name) {
		return uni.getStorageSync(name);
	},
	_ShowToast: function(content, _callback) {
		const time = 1500;
		uni.showToast({
			title: content,
			icon: "none",
			duration: time
		})
		if (_callback) {
			setTimeout(function() {
				return _callback()
			}, time)
		}
	},
	_ShowLoading: function(content) {
		uni.showLoading({
			title: content
		})
	},
	_HideLoading: function() {
		uni.hideLoading();
	},
	_GoBack: function(num = 1) {
		uni.navigateBack({
			delta: num
		})
	},
	_NavTo: function(url, data, method) {
		let nav_url = `/pages/${url}/${url}`;
		if (data) {
			nav_url += '?'
			for (let key in data) {
				if (nav_url[nav_url.length - 1] == '?') {
					nav_url += `${key}=${data[key]}`
				} else {
					nav_url += `&${key}=${data[key]}`
				}
			}
		}
		switch (method) {
			case this.navToMethod[0]:
				uni.navigateTo({
					url: nav_url
				})
				break;
			case this.navToMethod[1]:
				uni.redirectTo({
					url: nav_url
				})
				break;
			case this.navToMethod[2]:
				uni.reLaunch({
					url: nav_url
				})
				break;
			default:
				uni.navigateTo({
					url: nav_url
				})
				break;
		}
	},
	navToMethod: {
		0: 'navigateTo', //保留当前页面，跳转到应用内的某个页面。
		1: 'redirectTo', //关闭当前页面，跳转到应用内的某个页面。
		2: 'reLaunch' //关闭所有页面，打开到应用内的某个页面。
	},

	timeStampToTime: function(stamp) {
		// 时间戳转时间
		let date = new Date(stamp);
		// console.log(date);
		return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日 ${(date.getHours().toString().length == 1) ? ('0' + date.getHours()) : date.getHours()}:${(date.getMinutes().toString().length == 1) ? ('0' + date.getMinutes()) : date.getMinutes()}:${(date.getSeconds().toString().length == 1) ? ('0' + date.getSeconds()) : date.getSeconds()}`
	},

	timeStampToHourTime: function(hour_stamp) {
		// 时间戳转小时
		let date = new Date(Number(hour_stamp) - (8 * 3600 * 1000));
		return `${(date.getHours().toString().length == 1) ? ('0' + date.getHours()) : date.getHours()}:${(date.getMinutes().toString().length == 1) ? ('0' + date.getMinutes()) : date.getMinutes()}`
	},

	timeStampToYearDay: function(stamp) {
		// 时间戳转年月日
		let date = new Date(stamp);
		return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
	},

	// 转换数字为大写
	numberToChangeNum: function(num) {
		let changeNumArr = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
		if (num < 0 || num >= 10) {
			return false;
		}
		return changeNumArr[num];
	},

	// 根据区id获取地址详情
	getAddrById: function(id) {
		if(!id){
			return false;
		}
		id = Number(id).toString();
		if (id.length != 6) {
			return false;
		}
		// console.log(Region);
		let province =	Region[0][id[0] + id[1] + '0000'];
		let city = Region['0,'+ id[0] + id[1] + '0000'][id[0] + id[1] + id[2] + id[3] + '00'];
		let area = Region['0,'+ id[0] + id[1] + '0000'+ ',' + id[0] + id[1] + id[2] + id[3] + '00'][id];
		// console.log(province, city, area);
		return province+ ',' + city + ',' + area
	},
}